import 'package:flutter/material.dart';
import 'package:osc/common/constants.dart';
import 'package:osc/data/repository/repository.dart';
import 'package:osc/models/entity/app_update_info.dart';
import 'package:osc/res/colors.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/res/strings.dart';
import 'package:osc/ui/dialog/loading_dialog.dart';
import 'package:osc/ui/widgets/common_scaffold.dart';
import 'package:osc/utils/pd_util.dart';

class AboutAppPage extends StatefulWidget {
  @override
  _AboutAppPageState createState() => new _AboutAppPageState();
}

class _AboutAppPageState extends State<AboutAppPage> {
  bool isShowingDialog = false;


  @override
  Widget build(BuildContext context) {
    return new CommonScaffold(
      title: '${StringIds.aboutApp}${AppConfig.appName}',
      body: Column(
        children: <Widget>[
          Padding(
            padding: EdgeInsets.only(top: 30, bottom: Dimens.gap_view_normal),
            child: Image.asset(PdUtil.getImagePath("icon_logo"), width: 100, height: 100, fit: BoxFit.cover,),
          ),
          Text("${StringIds.currentVersion}V${AppConfig.version}", style: TextStyle(color: Colours.gray_66, fontSize: Dimens.font_sp14),),
          Container(
            color: Colors.white,
            margin: EdgeInsets.only(top: Dimens.gap_activity_vertical),
            padding: EdgeInsets.symmetric(horizontal: Dimens.gap_activity_horizontal),
            height: Dimens.view_item_height_normal,
            child: GestureDetector(
              onTap: (){
                checkUpdate();
              },
              child: Row(
                children: <Widget>[
                  Expanded(child: Text(StringIds.checkNewVersion, style: TextStyle(color: Colours.gray_33, fontSize: Dimens.font_sp12),)),
                  buildArrowRight(),
                ],
              ),
            ),
          )
        ],
      ),
    );
  }

  buildArrowRight(){
    return Padding(
      padding: EdgeInsets.only(left: Dimens.gap_view_normal),
      child: Image.asset("images/ic_arrow_right.png", width: 10,),
    );
  }

  /*showLoadingDialog(String msg){
    if(isShowingDialog){
      return;
    }
    isShowingDialog = true;
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: false,
        builder: (BuildContext context) {
          return new LoadingDialog( //调用对话框
            msg: msg,
            dismissDialogEvent: checkUpdate(),
          );
        });
  }*/

  showLoadingDialog(String msg, Function func){
    if(isShowingDialog){
//      return;
    }
    isShowingDialog = true;
    showDialog<Null>(
        context: context, //BuildContext对象
        barrierDismissible: true,
        builder: (BuildContext context) {
          return new LoadingDialog( //调用对话框
            msg: msg,
            dismissDialogEvent: func,
          );
        });
  }

  /*dismissLoadingDialog(){
    if(isShowingDialog){
      isShowingDialog = false;
      Navigator.pop(context);
    }
  }*/

  /*checkUpdate() async{
    showLoadingDialog("正在检查更新...");
    await HttpRepository().checkUpdate().then((result){
      dismissLoadingDialog();
      if(result != null && result.success){
        AppUpdateInfo appUpdateInfo = result.result;
        if(appUpdateInfo != null){
          showNewestTips();
        } else {
          showNewestTips();
        }
      } else {
        showNewestTips();
      }
    });
  }*/
  checkUpdate() async{
    showLoadingDialog("正在检查更新...", checkUpdate2);
  }
  checkUpdate2(Function func) async{
    await HttpRepository().checkUpdate().then((result){
      if(result != null && result.success){
        AppUpdateInfo appUpdateInfo = result.result;
        if(appUpdateInfo != null){
          showNewestTips();
        } else {
          showNewestTips();
        }
      } else {
        showNewestTips();
      }
      func();
    });
  }


  showTips(String msg){
    PdUtil.showToast(msg: msg);
  }
  showNewestTips(){
    showTips("当前已是最新版本");
  }
}
